package com.rdgk.newtech.service.impl;

import com.rdgk.newtech.dao.*;
import com.rdgk.newtech.service.AuthorAnalyseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class AuthorAnalyseServiceImpl implements AuthorAnalyseService {
    @Autowired
    private TechniqueArticleDao techArticleDao;
    @Autowired
    private PaperDao paperDao;
    @Autowired
    private PatentDao patentDao;
    @Autowired
    private CraOrganizationDao organizationDao;
    @Autowired
    private TechniqueWordDao techniqueWordDao;
    @Autowired
    private CraAuthorDao authorDao;



    @Override
    public List<Map<String,Object>> importantAuthorList(Integer topicId, String[] words) {//
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("topicId",topicId);
        return authorDao.getImportantAuthors(params);
    }

    @Override
    public List<Map<String,Object>> technologyRegionalDistribution(Integer topicId, String[] words) {
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("topicId",topicId);
        return authorDao.statisticsAuthorNumForMap(params);
    }

    @Override
    public Map<String, Object> allAuthorCooperateNet(Integer topicId, String keyword) {
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("topicId",topicId);
        params.put("keyword",keyword);
        Map<String,Object> results = new HashMap<String,Object>();
        List<Map<String,Object>> nodes = new ArrayList<Map<String,Object>>();//节点列表
        List<Map<String,Object>> edges = new ArrayList<Map<String,Object>>();//关系列表

        List<Map<String,Object>> techniqueArticleNodes = new ArrayList<Map<String,Object>>();//网络新闻动态节点
        List<Map<String,Object>> paperNodes = new ArrayList<Map<String,Object>>();//论文节点
        List<Map<String,Object>> patentNodes = new ArrayList<Map<String,Object>>();//专利节点
        List<Map<String,Object>> authorNodes = new ArrayList<Map<String,Object>>();//专家节点


        List<Map<String,Object>> authorTechEdges = new ArrayList<Map<String,Object>>();//专家与网络新闻动态关系
        List<Map<String,Object>> authorPaperEdges = new ArrayList<Map<String,Object>>();//专家与论文关系
        List<Map<String,Object>> authorPatentEdges = new ArrayList<Map<String,Object>>();//专家与专利关系

        List<Map<String,Object>> hotwordArticle = techArticleDao.getArticleByHotword(params);
        List<Object> articleIdParams = new ArrayList<Object>();//用于传递参数的网络新闻动态id的集合
        for (Map<String,Object> article:hotwordArticle
             ) {
            if(!articleIdParams.contains(article.get("articleid"))){
                Map<String,Object> techniqueArticle = new HashMap<String,Object>();//创建网络动态节点
                techniqueArticle.put("id","tech_"+article.get("articleid"));
                techniqueArticle.put("label",article.get("title"));
                techniqueArticleNodes.add(techniqueArticle);
                articleIdParams.add(article.get("articleid"));//保存每个网络新闻动态的id
            }
        }

        List<Map<String,Object>> hotwordPaper = paperDao.getPaperByHotword(params);
        List<Object> paperIdParams = new ArrayList<Object>();//用于传递参数的论文id的集合
        for (Map<String,Object> paper:hotwordPaper
                ) {
            if(!paperIdParams.contains(paper.get("paperid"))){
                Map<String,Object> papernode = new HashMap<String,Object>();//创建论文节点
                papernode.put("id","paper_"+paper.get("paperid"));
                papernode.put("label",paper.get("title"));
                paperNodes.add(papernode);
                paperIdParams.add(paper.get("paperid"));
            }
        }

        List<Map<String,Object>> hotwordPatent = patentDao.getPatentByHotword(params);
        List<Object> patentIdParams = new ArrayList<Object>();//用于传递参数的论文id的集合
        for (Map<String,Object> patent:hotwordPatent
                ) {
            if(!patentIdParams.contains(patent.get("patentid"))){
                Map<String,Object> patendnode = new HashMap<String,Object>();//创建专利节点
                patendnode.put("id","patent_"+patent.get("patentid"));
                patendnode.put("label",patent.get("title"));
                patentNodes.add(patendnode);
                patentIdParams.add(patent.get("patentid"));
            }
        }

        params.put("relArticleList",articleIdParams);
        params.put("relPaperList",paperIdParams);
        params.put("relPatentList",patentIdParams);
        List<Map<String,Object>> relAuthors = authorDao.getAuthorByHotword(params);
        List<Object> tmpAuthorIds = new ArrayList<Object>();//临时存放专家id的list，用于去重
        for (Map<String,Object> relAuthor:relAuthors
             ) {
            Object authorid = relAuthor.get("authorid");
            if(!tmpAuthorIds.contains(authorid)){//判断专家id是否已经出现过
                Map<String,Object> authorNode = new HashMap<String,Object>();//创建专家节点
                authorNode.put("id","author_"+relAuthor.get("authorid"));
                authorNode.put("label",relAuthor.get("name"));
                authorNodes.add(authorNode);
                tmpAuthorIds.add(authorid);//标记该专家已经出现过
            }
            if("0".equals(relAuthor.get("reltype"))){//创建专家与网络动态关系
                Map<String,Object> authorArticleEdge = new HashMap<String,Object>();
                authorArticleEdge.put("sourceID","author_"+relAuthor.get("authorid"));
                authorArticleEdge.put("targetID","tech_"+relAuthor.get("targetID"));
                authorTechEdges.add(authorArticleEdge);
            }
            if("1".equals(relAuthor.get("reltype"))){//创建专家与论文关系
                Map<String,Object> authorPaperEdge = new HashMap<String,Object>();
                authorPaperEdge.put("sourceID","author_"+relAuthor.get("authorid"));
                authorPaperEdge.put("targetID","paper_"+relAuthor.get("targetID"));
                authorPaperEdges.add(authorPaperEdge);
            }
            if("2".equals(relAuthor.get("reltype"))) {//创建专家与专利关系{
                Map<String, Object> authorPatentEdge = new HashMap<String, Object>();
                authorPatentEdge.put("sourceID","author_"+relAuthor.get("authorid"));
                authorPatentEdge.put("targetID","patent_"+relAuthor.get("targetID"));
                authorPatentEdges.add(authorPatentEdge);
            }
        }

        //将所有节点增加到nodes集合中
        nodes.addAll(authorNodes);
        nodes.addAll(techniqueArticleNodes);
        nodes.addAll(paperNodes);
        nodes.addAll(patentNodes);
        //将所有关系增加到edges集合中
        edges.addAll(authorTechEdges);
        edges.addAll(authorPaperEdges);
        edges.addAll(authorPatentEdges);
        //将关系和节点列表添加到结果集中
        results.put("nodes",nodes);
        results.put("edges",edges);
        return results;
    }
    @Override
    public Map<String,Object>  authorDetail(Integer topicId,Integer authorId){
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("topicId",topicId);
        params.put("authorId",authorId);
        Map<String,Object> results = new HashMap<String,Object>();
        List<Map<String,Object>> nodes = new ArrayList<Map<String,Object>>();//节点列表
        List<Map<String,Object>> edges = new ArrayList<Map<String,Object>>();//关系列表

        List<Map<String,Object>> techniqueArticleNodes = new ArrayList<Map<String,Object>>();//网络新闻动态节点
        List<Map<String,Object>> paperNodes = new ArrayList<Map<String,Object>>();//论文节点
        List<Map<String,Object>> patentNodes = new ArrayList<Map<String,Object>>();//专利节点
        List<Map<String,Object>> authorNodes = new ArrayList<Map<String,Object>>();//专家节点


        List<Map<String,Object>> authorTechEdges = new ArrayList<Map<String,Object>>();//专家与网络新闻动态关系
        List<Map<String,Object>> authorPaperEdges = new ArrayList<Map<String,Object>>();//专家与论文关系
        List<Map<String,Object>> authorPatentEdges = new ArrayList<Map<String,Object>>();//专家与专利关系

        List<Object> authorTechEdgeIds = new ArrayList<Object>();//专家与网络新闻动态关系
        List<Object> authorPaperEdgeIds = new ArrayList<Object>();//专家与论文关系
        List<Object> authorPatentEdgeIds = new ArrayList<Object>();//专家与专利关系

        List<Object> articleIdParams = new ArrayList<Object>();//用于传递参数的网络新闻动态id的集合
        List<Object> paperIdParams = new ArrayList<Object>();//用于传递参数的论文id的集合
        List<Object> patentIdParams = new ArrayList<Object>();//用于传递参数的论文id的集合
        List<Object> authorIdParams = new ArrayList<Object>();//用于传递参数的专家id的集合

        List<Map<String,Object>> authorfos = authorDao.getAuthorByID(params);
        Map<String,Object> authordetail = authorfos.size()>0?authorfos.get(0):null;//机构详情
        Map<String,Object> authorNode = new HashMap<String,Object>();
        authorNode.put("id","author_"+authordetail.get("id"));
        authorNode.put("label",authordetail.get("NAME"));
        authorNodes.add(authorNode);
        authorIdParams.add(authordetail.get("id"));

        List<Map<String,Object>> hotworddata = new ArrayList<Map<String,Object>>();//机构相关热词
        hotworddata = authorDao.getAuthorHotwords(params);

        List<Map<String,Object>> orgs = new ArrayList<Map<String,Object>>();//机构相关专家
        orgs = authorDao.getAuthorOrgs(params);

        List<Map<String,Object>> authorTechEvents = new ArrayList<Map<String,Object>>();//专家相关技术动态
        authorTechEvents = authorDao.getAuthorTechEvents(params);
        for (Map<String,Object> authorTechEvent:authorTechEvents
                ) {
            if("0".equals(authorTechEvent.get("reltype"))){//创建专家与论文关系
                Map<String,Object> echniqueArticleNode = new HashMap<String,Object>();
                if(!articleIdParams.contains(authorTechEvent.get("id"))) {
                    echniqueArticleNode.put("id","tech_"+authorTechEvent.get("id"));
                    echniqueArticleNode.put("label",authorTechEvent.get("title"));
                    techniqueArticleNodes.add(echniqueArticleNode);
                    articleIdParams.add(authorTechEvent.get("id"));
                }
                Map<String,Object> authorTechEdge = new HashMap<String,Object>();
                if(!authorTechEdgeIds.contains("author_" + authordetail.get("id")+"rel"+"tech_" + authorTechEvent.get("id"))) {
                    authorTechEdge.put("sourceID", "author_" + authordetail.get("id"));
                    authorTechEdge.put("targetID", "tech_" + authorTechEvent.get("id"));
                    authorTechEdges.add(authorTechEdge);
                    authorTechEdgeIds.add("author_" + authordetail.get("id")+"rel"+"tech_" + authorTechEvent.get("id"));
                }
            }
            if("1".equals(authorTechEvent.get("reltype"))){//创建专家与论文关系
                Map<String,Object> paperNode = new HashMap<String,Object>();
                if(!paperIdParams.contains(authorTechEvent.get("id"))) {
                    paperNode.put("id", "paper_"+authorTechEvent.get("id"));
                    paperNode.put("label", authorTechEvent.get("title"));
                    paperNodes.add(paperNode);
                    paperIdParams.add(authorTechEvent.get("id"));
                }
                Map<String,Object> authorPaperEdge = new HashMap<String,Object>();
                if(!authorPaperEdgeIds.contains("author_"+authordetail.get("id")+"rel"+"paper_"+authorTechEvent.get("id"))) {
                    authorPaperEdge.put("sourceID","author_"+authordetail.get("id"));
                    authorPaperEdge.put("targetID","paper_"+authorTechEvent.get("id"));
                    authorPaperEdges.add(authorPaperEdge);
                    authorPaperEdgeIds.add("author_"+authordetail.get("id")+"rel"+"paper_"+authorTechEvent.get("id"));
                }
            }
            if("2".equals(authorTechEvent.get("reltype"))) {//创建专家与专利关系{
                Map<String, Object> patentNode = new HashMap<String, Object>();
                if(!patentIdParams.contains(authorTechEvent.get("id"))) {
                    patentNode.put("id", "patent_"+authorTechEvent.get("id"));
                    patentNode.put("label", authorTechEvent.get("title"));
                    patentNodes.add(patentNode);
                    patentIdParams.add(authorTechEvent.get("id"));
                }
                Map<String,Object> authorPatentEdge = new HashMap<String,Object>();
                if(!authorPatentEdgeIds.contains("author_"+authordetail.get("id")+"rel"+"paper_" + authorTechEvent.get("id"))) {
                    authorPatentEdge.put("sourceID","author_"+authordetail.get("id"));
                    authorPatentEdge.put("targetID","patent_"+authorTechEvent.get("id"));
                    authorPatentEdges.add(authorPatentEdge);
                    authorPatentEdgeIds.add("author_"+authordetail.get("id")+"rel"+"paper_" + authorTechEvent.get("id"));
                }
            }
        }

        params.put("relArticleList",articleIdParams);
        params.put("relPaperList",paperIdParams);
        params.put("relPatentList",patentIdParams);

        List<Map<String,Object>> relatedAuthors =  authorDao.getAuthorRelatedAuthors(params);
        for (Map<String,Object> relatedAuthor:relatedAuthors
                ) {
            if("0".equals(relatedAuthor.get("reltype"))){//创建专家与论文关系
                Map<String,Object> relatedAuthorNode = new HashMap<String,Object>();
                if(!authorIdParams.contains(relatedAuthor.get("caid"))) {
                    relatedAuthorNode.put("id", "author_"+relatedAuthor.get("caid"));
                    relatedAuthorNode.put("label", relatedAuthor.get("caname"));
                    authorNodes.add(relatedAuthorNode);
                    authorIdParams.add(relatedAuthor.get("caid"));
                }
                Map<String,Object> authorTechEdge = new HashMap<String,Object>();
                if(!authorTechEdgeIds.contains("author_" + relatedAuthor.get("caid")+"rel"+"tech_" + relatedAuthor.get("id"))) {
                    authorTechEdge.put("sourceID", "author_" + relatedAuthor.get("caid"));
                    authorTechEdge.put("targetID", "tech_" + relatedAuthor.get("id"));
                    authorTechEdges.add(authorTechEdge);
                    authorTechEdgeIds.add("author_" + relatedAuthor.get("caid")+"rel"+"tech_" + relatedAuthor.get("id"));
                }

            }
            if("1".equals(relatedAuthor.get("reltype"))){//创建专家与论文关系
                Map<String,Object> relatedAuthorNode = new HashMap<String,Object>();
                if(!authorIdParams.contains(relatedAuthor.get("caid"))) {
                    relatedAuthorNode.put("id", "author_"+relatedAuthor.get("caid"));
                    relatedAuthorNode.put("label", relatedAuthor.get("caname"));
                    authorNodes.add(relatedAuthorNode);
                    authorIdParams.add(relatedAuthor.get("caid"));
                }
                Map<String,Object> authorPaperEdge = new HashMap<String,Object>();
                if(!authorPaperEdgeIds.contains("author_" + relatedAuthor.get("caid")+"rel"+"paper_" + relatedAuthor.get("id"))) {
                    authorPaperEdge.put("sourceID", "author_" + relatedAuthor.get("caid"));
                    authorPaperEdge.put("targetID", "paper_" + relatedAuthor.get("id"));
                    authorPaperEdges.add(authorPaperEdge);
                    authorPaperEdgeIds.add("author_" + relatedAuthor.get("caid")+"rel"+"paper_" + relatedAuthor.get("id"));
                }
            }
            if("2".equals(relatedAuthor.get("reltype"))) {//创建专家与专利关系{
                Map<String,Object> relatedAuthorNode = new HashMap<String,Object>();
                if(!authorIdParams.contains(relatedAuthor.get("caid"))) {
                    relatedAuthorNode.put("id", "author_"+relatedAuthor.get("caid"));
                    relatedAuthorNode.put("label", "patent_"+relatedAuthor.get("caname"));
                    authorNodes.add(relatedAuthorNode);
                    authorIdParams.add(relatedAuthor.get("caid"));
                }
                Map<String,Object> authorPatentEdge = new HashMap<String,Object>();
                if(!authorPatentEdgeIds.contains("author_" + relatedAuthor.get("coid")+"rel"+"patent_" + relatedAuthor.get("id"))) {
                    authorPatentEdge.put("sourceID", "author_" + relatedAuthor.get("coid"));
                    authorPatentEdge.put("targetID", "patent_" + relatedAuthor.get("id"));
                    authorPatentEdges.add(authorPatentEdge);
                    authorPatentEdgeIds.add("author_" + relatedAuthor.get("coid")+"rel"+"patent_" + relatedAuthor.get("id"));
                }
            }
        }

        //将所有节点增加到nodes集合中
        nodes.addAll(authorNodes);
        nodes.addAll(techniqueArticleNodes);
        nodes.addAll(paperNodes);
        nodes.addAll(patentNodes);
        //将所有关系增加到edges集合中
        edges.addAll(authorTechEdges);
        edges.addAll(authorPaperEdges);
        edges.addAll(authorPatentEdges);
        //将关系和节点列表添加到结果集中
        results.put("nodes",nodes);
        results.put("edges",edges);

        results.put("authordetail",authordetail);//机构详细信息
        results.put("hotwords",hotworddata);//机构相关热词
        results.put("orgs",orgs);//机构相关专家
        results.put("articelNum",articleIdParams.size());//专家相关网络新闻数量
        results.put("paperNum",paperIdParams.size());//专家相关论文数量
        results.put("patentNum",patentIdParams.size());//专家相关论文数量
        return results;
    }
}
